<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="js/JsonExportExcel.min.js"></script>
    <script typet="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
    <title>Document Demo</title>
</head>
<body>
<button onclick="getExcel()">点击我下载表格</button>

</body>
<script>
    var data = JSON.stringify({
        "pageSize": 1000,
        "pageNo": 1
    });

    var xhr = new XMLHttpRequest();
    xhr.withCredentials = true;

    xhr.addEventListener("readystatechange", function () {
        if (this.readyState === 4) {
            console.log(this.responseText);
        }
    });

    xhr.open("POST", "https://tpms-cn.tuya-inc.com:7799/api/customerOwner/getCustomerList");
    xhr.setRequestHeader("Referer", "https://tpms-cn.tuya-inc.com:7799/demand/customer/list");
    xhr.setRequestHeader("Origin", "https://tpms-cn.tuya-inc.com:7799");
    xhr.setRequestHeader("Content-Type", "application/json");


    function getExcel() {
        xhr.send(data);

        var str = "{\"success\":true,\"msg\":\"请求成功\",\"result\":{\"total\":123,\"pageIndex\":0,\"limit\":0,\"content\":[{\"customerCode\":\"TYKH20200607nNdX\",\"customerName\":\"深圳市芯中芯科技有限公司\",\"customerLevel\":\"D-普通客户\",\"customerDesc\":null,\"followStatus\":16,\"followStatusValue\":\"已成交客户-已成交复购客户\",\"bdList\":[{\"staffId\":\"00296\",\"name\":\"韩庄\",\"tuyaName\":\"帮主\",\"tuyaNameFullString\":\"(帮主)韩庄\"}],\"buLeaderList\":null,\"mainPMList\":[{\"staffId\":\"1376381567995248640\",\"name\":\"顾毅生\",\"tuyaName\":\"Doctor\",\"tuyaNameFullString\":\"(Doctor)顾毅生\"}],\"pmList\":[{\"staffId\":\"1298440599753576448\",\"name\":\"何小曼\",\"tuyaName\":\"南夕\",\"tuyaNameFullString\":\"(南夕)何小曼\"},{\"staffId\":\"00560\",\"name\":\"钟颖\",\"tuyaName\":\"Zoe\",\"tuyaNameFullString\":\"(Zoe)钟颖\"},{\"staffId\":\"00355\",\"name\":\"邹依依\",\"tuyaName\":\"Eleven\",\"tuyaNameFullString\":\"(Eleven)邹依依\"},{\"staffId\":\"00915\",\"name\":\"李志山\",\"tuyaName\":\"谷雨\",\"tuyaNameFullString\":\"(谷雨)李志山\"},{\"staffId\":\"1268356647647047680\",\"name\":\"俞文杰\",\"tuyaName\":\"东君\",\"tuyaNameFullString\":\"(东君)俞文杰\"},{\"staffId\":\"1335771160389926912\",\"name\":\"刘小銮\",\"tuyaName\":\"安迪\",\"tuyaNameFullString\":\"(安迪)刘小銮\"},{\"staffId\":\"1329247361041473536\",\"name\":\"王婷\",\"tuyaName\":\"Rhea\",\"tuyaNameFullString\":\"(Rhea)王婷\"},{\"staffId\":\"1381434024068718592\",\"name\":\"褚孝昆\",\"tuyaName\":\"开山\",\"tuyaNameFullString\":\"(开山)褚孝昆\"},{\"staffId\":\"00607\",\"name\":\"黄沙丽\",\"tuyaName\":\"Sally\",\"tuyaNameFullString\":\"(Sally)黄沙丽\"},{\"staffId\":\"02359\",\"name\":\"王磊\",\"tuyaName\":\"林斐\",\"tuyaNameFullString\":\"(林斐)王磊\"},{\"staffId\":\"1305337535018422272\",\"name\":\"田梦情\",\"tuyaName\":\"小埋\",\"tuyaNameFullString\":\"(小埋)田梦情\"},{\"staffId\":\"00096\",\"name\":\"徐威扬\",\"tuyaName\":\"折颜\",\"tuyaNameFullString\":\"(折颜)徐威扬\"}],\"techSupportList\":null,\"focusList\":null,\"canCreate\":false,\"canCreateTask\":false,\"valid\":true}]}}";
        var res = JSON.parse(str);
        var index = 1;
        var list = [];
        res.result.content.forEach(ele => {
            var col = {
                "no": index++,
                "customerName": ele.customerName,
                "customerLevel": ele.customerLevel,
                "mainPMName": ele.mainPMList[0].name,
                "bdName": ele.bdList[0].name

            };

            list.push(col);
        });


        var option = {};
        option.fileName = 'excel'
        option.datas = [
            {
                sheetData: list,
                sheetName: 'sheet',
                sheetFilter: ['no', 'customerName', 'customerLevel', 'mainPMName', 'bdName'],
                sheetHeader: ['序号', '客户名称', '客户等级', '主PM', '商务']
            }
        ];
        var toExcel = new ExportJsonExcel(option);
        toExcel.saveExcel();
    }
</script>
</html>